<template>
  <div>
    <el-row>
      <el-col :span="4">
        <div class="tree">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>
          <el-tree @node-click="handlerClick" class="filter-tree" :data="EngData" :props="defaultProps"
            :filter-node-method="filterNode" 
            ref="tree">
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="params.device_id" class="table">
          <el-form :inline="true" :model="form">

            <el-form-item label="选择月份：">
              <el-date-picker v-model="form.month" type="month" value-format="yyyy-MM" placeholder="选择月份" align="right">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="toSearch">查询月报</el-button>
            </el-form-item>

          </el-form>

          <!-- 表格 -->
          <el-table :data="monitorData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column prop="month" label="月份" width="180" align="center">
                            <template v-slot="scope">
                                <div>
                                    {{ scope.row.insert_time | fmtDate }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="tsp" label="tsp" width="180">
                        </el-table-column>
                        <el-table-column prop="pm10" label="pm10">
                        </el-table-column>
                        <el-table-column prop="pm25" label="pm2.5">
                        </el-table-column>
                        <el-table-column prop="temperature" label="温度">
                        </el-table-column>
                        <el-table-column prop="humidity" label="湿度">
                        </el-table-column>
                        <el-table-column prop="wind_direction" label="风向">
                        </el-table-column>
                        <el-table-column prop="wind_speed" label="风速">
                        </el-table-column>
                    </el-table>

      
        </div>
        <div v-else style="line-height: 50px;color: red;font-size: 20px;text-align: center;">请点击设备查看月报数据</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>

import { get } from '@/utils/request'

export default {
  data() {
    return {
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      monitorData: [],
      form: {
        month:''
      },
      EngData: [],
      

      params: {
         
                device_id:0,
                engineer_id:0 
      },
      
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    handlerClick(val, node) {
      if (!val.children) {
        this.params.device_id = val.id;
        this.params.engineer_id = val.engineer_id;
       // this.toSearch();
       this.getMonthly();
      }
    },

    toSearch() {
      this.params.page = 1;
      this.getMonthly();
     // this.getMonthly();      
      // if (!this.form.month) {
      //   this.$message.warning('请选择月份');
      //   return;
    },

    //async getMonthlyReport() {
      async getMonthly() {
        let temp = {
                ...this.params
            };
            // 如果传了时间  
            if(this.form.month){
                temp.date = this.form.month;
            }
            let res = await get('/monitor/queryMouthData',temp);
            this.monitorData = res.data;
      },


    async getEngDevData() {
      let res = await get('/engineer/findEngineerDeviceTree');
            this.EngData = res.data;
      // try {
      //   const res = await get('/engineer/findEngineerDeviceTree');
      //   this.EngData = res.data || [];
      // } catch (err) {
      //   this.$message.error('获取设备树失败：' + err.message);
      // }
    },


    filterNode(value, data) {
      if (!value) return true;
       return data.name.indexOf(value) !== -1;
      // return data.name?.toLowerCase().indexOf(value.toLowerCase()) !== -1;
    },

    tableRowClassName({ row,rowIndex }) {
       if (rowIndex % 2 == 0 || rowIndex == 0) {
                return 'warning-row';
            } else {
                return 'success-row';
            }
            return '';
      // return rowIndex % 2 === 0 ? 'warning-row' : 'success-row';
    }
  },
   created() {
        this.getEngDevData();
        this.getMonthly();
      
    }

  // mounted() {
  //   this.getEngDevData();
  //    
  // },
  // beforeDestroy() {
  //    
  // }
};
</script>

<style lang="scss" scoped>

.tree {
  height: calc(100vh - 60px);
  padding-right: 10px;
  border-right: 1px solid #ececec;
  margin-right: 10px;
}

// .table {
//   padding: 20px;
// }


::v-deep .el-table .warning-row {
  background: oldlace;
}

::v-deep .el-table .success-row {
  background: #f0f9eb;
}
</style>
